package com.zb.service.imp.base;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zb.dao.ext.system.OmsOperListDao;
import com.zb.dao.ext.system.OmsOperMerchantBranchDao;
import com.zb.entity.base.JsonCommon;
import com.zb.entity.base.JsonForList;
import com.zb.entity.base.OmsCommonParam;
import com.zb.entity.base.OmsEntityEnum;
import com.zb.entity.exception.BizException;
import com.zb.entity.exception.OmsDaoException;
import com.zb.entity.system.OmsOperMerchantBranch;
import com.zb.entity.system.OmsRole;
import com.zb.service.base.OperMerchantBranchService;
import com.zb.util.OmsConstant.MsgCodeEnum;
@Service @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class OperMerchantBranchServiceImpl extends BaseService implements OperMerchantBranchService{
	@Autowired
	private OmsOperMerchantBranchDao operMerchantBranchDao;
	@Autowired
	private OmsOperListDao operListDao;
	@Override
	public synchronized JsonCommon<OmsOperMerchantBranch> addOmsOperMerchantBranch(OmsOperMerchantBranch oom) {
		OmsOperMerchantBranch omsOperMerchantBranchTmp = new OmsOperMerchantBranch();
		omsOperMerchantBranchTmp.setOper_name(oom.getOper_name());
		OmsOperMerchantBranch omsOperMerchantBranchQury = operMerchantBranchDao.findOneExample(omsOperMerchantBranchTmp, null, null);
		if (omsOperMerchantBranchQury != null) {
			return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "分公司名称重复");
		}
		
		String currentId = this.genarateOmb(oom.getOper_merchant_id());
		if (currentId == null)
		{
			return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "genarate id error!!!");
		}
		oom.setOper_merchant_branch_id(currentId);
		try 
		{
			this.operMerchantBranchDao.add(oom);
			return this.buildSuccessJsonCommon(oom);
		} catch (OmsDaoException e) 
		{
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonCommon<OmsOperMerchantBranch> updateOmsOperMerchantBranch(OmsOperMerchantBranch oom) {
		try 
		{
			//分公司名称不能重复
			OmsOperMerchantBranch omsOperMerchantBranchTmp = new OmsOperMerchantBranch();
		    String condition = " where oper_merchant_branch_id <>" + oom.getOper_merchant_branch_id() + " and oper_name = '" + oom.getOper_name() + "'";
		    List<OmsOperMerchantBranch> listOmsOperMerchantBranch = operMerchantBranchDao.findListByCondition(omsOperMerchantBranchTmp, condition);
			if (listOmsOperMerchantBranch.size() >0 ) {
				return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "分公司名称重复!");
			}
			operMerchantBranchDao.update(oom);
		} 
		catch (OmsDaoException e) 
		{
			logError(e.getMessage(),e);
			return this.buildError400JsonCommon();
		}
		return this.buildSuccessJsonCommon(oom);
	}

	@Override
	public JsonCommon<?> deleteOmsOperMerchantBranch(String id) {
		try 
		{
			operMerchantBranchDao.deleteById(id, OmsOperMerchantBranch.class);
			//分支机构被删除，相关用户被删除。
			operListDao.logicDelOperByEntity(OmsEntityEnum.OPRERMERCHANT_BRANCH.getValue(), id);
		} 
		catch (OmsDaoException e) 
		{
			logError(e.getMessage(),e);
			return this.buildError400JsonCommon();
		}
		return this.buildSuccessJsonCommon("success");
	}

	@Override
	public JsonForList<?> findPageOmsOperMerchantBranch(OmsCommonParam param,OmsOperMerchantBranch oom) {
		try 
		{
			List<OmsOperMerchantBranch> lst = operMerchantBranchDao.pagingFind(param, oom);
			int total = 0;
			if (param.isQueryTotal())
			{
				total = operMerchantBranchDao.queryTotalByObject(oom);
			}
			return this.buildSuccessJsonForList(param, lst, total);
		} catch (Exception e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonForList();
		}
	}
	
	private String genarateOmb(String oper_merchant_id )
	{
		try 
		{
			//String maxId = operMerchantBranchDao.findMaxId(OmsOperMerchantBranch.class);
			//return this.genarateId(maxId, 0);
			String maxId = operMerchantBranchDao.findMaxPkIdByCondition(13, 6, "where oper_merchant_id = '"+oper_merchant_id+"'", OmsOperMerchantBranch.class);
			return oper_merchant_id+"00"+this.genarateId(maxId, 6)+"00";
		}
		catch (OmsDaoException e) 
		{
			this.logError(e.getMessage(), e);
			return null;
		}
		
	}

	@Override
	public JsonCommon<?> findObj(String id) {
		if (StringUtils.isEmpty(id))
		{
			return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:the request id is empty");
		}
		try 
		{
			OmsOperMerchantBranch op = operMerchantBranchDao.findOmbWithId(id);
			return this.buildSuccessJsonCommon(op);
		} 
		catch (Exception e) 
		{
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

}
